package ch01.SL;

import com.bdqn.Person;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class GetClassMethodsInfo {


    public static void main(String[] args) {
        Method[] methods = Person.class.getDeclaredMethods();

        System.out.println("============方法展示============");

        for (Method method : methods) {

            System.out.println("方法名：" + method.getName());

            System.out.println("方法返回值类型：" + method.getReturnType());

            Class[] params = method.getParameterTypes();
            if (params.length == 0) {
                System.out.println("该方法没有参数");
            } else {
                System.out.println("该方法的参数列表为：[");
                for (int i = 0; i < params.length; i++) {
                    if (i != 0) {
                        System.out.print(",");
                    }
                    System.out.println(params[i].getName());
                }
                System.out.println("]");
            }
            System.out.println("访问修饰符");
            int modifier = method.getModifiers();
            if ((modifier & Modifier.PUBLIC) == Modifier.PUBLIC) {
                System.out.println("public");
            } else if ((modifier & Modifier.PROTECTED) == Modifier.PROTECTED) {
                System.out.println("protected");
            } else if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE) {
                System.out.println("private");
            } else {
                System.out.println("default(package)");
            }

            if ((modifier & Modifier.STATIC) == Modifier.STATIC) {
                System.out.println("这是一个静态方法");
            }
            if ((modifier & Modifier.FINAL) == Modifier.FINAL) {
                System.out.println("这是一个final方法");
            }
            if ((modifier & Modifier.ABSTRACT) == Modifier.ABSTRACT) {
                System.out.println("这是一个抽象方法");
            }
            if ((modifier & Modifier.SYNCHRONIZED) == Modifier.SYNCHRONIZED) {
                System.out.println("这是一个final方法");
            }
            Class declaringClass = method.getDeclaringClass();
            System.out.println("方法声明在" + declaringClass.getName() + "中");

            Class[] exceptions = method.getExceptionTypes();
            if (exceptions.length > 0) {
                System.out.println("该方法可能抛出的异常有:[");
                for (int i = 0; i < exceptions.length; i++) {
                    if (i != 0) {
                        System.out.print(",");
                    }
                    System.out.println(exceptions[i].getName());
                }
                System.out.println("]");
            }
            System.out.println("-------------------------------------");
        }
    }
}
